import requests
import parsel   # 解析数据
import os


head = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
}

url = 'https://www.jdlingyu.com/tuji'

html_data = requests.get(url, headers=head).text
# 解析数据拿到图集连接
selector_1 = parsel.Selector(html_data)
title_list = selector_1.css('#post-list ul li h2 a::text').getall()
link_list = selector_1.css('#post-list ul li h2 a::attr(href)').getall()
for title ,link in zip(title_list, link_list):
    print(f'正在爬取{title}')

    if not os.path.exists(f'img/{title}'):
        os.mkdir(f'img/{title}')

    # 发送请求
    resp = requests.get(link, headers=head)

    # 获取网页源代码
    html_data = resp.text

    # 解析数据
    selector = parsel.Selector(html_data)

    # ::attr()  提取标签属性名称
    ing_list =  selector.css('.entry-content img::attr(src)').getall()
    for img in ing_list:
        # 访问图片连接，并且获取图片二进制的数据
        ind_data = requests.get(img, headers=head).content
        img_name = img.split('/')[-1]
        with open(f'img/{title}/{img_name}', mode='wb') as f:
            f.write(ind_data)